<template>
    <view>
        <u-index-list :scroll-top="scrollTop" :sticky="false">
            <view v-for="(item, index) in indexList" :key="index" >
               <he-index-anchor :index="item"></he-index-anchor>
               <view class="he-index-row">
                    <view class="he-list-cell" @click="back(row)"  v-for="(row, key) in list[item]" :key="key" >
                        {{row.name}}
                    </view>
               </view>
		    </view>
        </u-index-list>
    </view>
</template>

<script>
import uIndexList from "../../components/u-index-list.vue";
import heIndexAnchor from "./components/he-index-anchor.vue";
export default {
    name: "logisticsCompany",
    data() {
        return {
            indexList: [
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
                "V", "W", "X", "Y", "Z"
            ],
            active: false,
            scrollTop: 0,
            list: {}
        }
    },
    mounted() {
        let _this = this;
        this.$heshop.search('post', {
            include: 'setting'
        }, {
            keyword: 'expressjson'
        }).then(function (response) {
            let list = response;
            list.sort((a, b) => 
                a.name.localeCompare(b.name, 'zh')
            );
            let data = _this.sortFriend(list);
            _this.list = data;
            console.log(_this.list);
        });
    },
    methods: {
        chineseToEnglish: function (c) {
            var idx = -1;
            var MAP = 'ABCDEFGHJKLMNOPQRSTWXYZ';
            var boundaryChar = '驁簿錯鵽樲鰒餜靃攟鬠纙鞪黁漚曝裠鶸蜶籜鶩鑂韻糳';
            if (!String.prototype.localeCompare) {
                throw Error('String.prototype.localeCompare not supported.');
            }
            if (/[^\u4e00-\u9fa5]/.test(c)) {
                return c;
            }
            for (var i = 0; i < boundaryChar.length; i++) {
                if (boundaryChar[i].localeCompare(c, 'zh-CN-u-co-pinyin') >= 0) {
                    idx = i;
                    break;
                }
            }
            return MAP[idx];
        },
        //获取第一个字母
        getFirstUpperChar: function(str){
            var string = String(str);
            var c = string.substr(0, 1);
            if (/[^\u4e00-\u9fa5]/.test(c)) {
                return c.toUpperCase();
            } else {
                return this.chineseToEnglish(c);
            }
        },
        sortFriend(list) {
            let map = {};
            let _this =this;
            var c = 'A'.charCodeAt();
            for(; c <= 'Z'.charCodeAt(); c++ ){
                map[String.fromCharCode(c)] = [];
            }
            map['#'] = [];
            var firstCharUpper;
            list.forEach(function(item){
                firstCharUpper = _this.getFirstUpperChar(item.name);
                if (map.hasOwnProperty(firstCharUpper)) {
                    map[firstCharUpper].push(item);
                } else {
                    map['#'].push(item);
                }
            });
            return map;
        },
        back: function (row) {
            uni.redirectTo({
                 url: '/pages/order/fill-return-information?name=' + row.name
            });
        }
    },
    components: {
        uIndexList,
        heIndexAnchor
    },
    onPageScroll(e) {
        this.scrollTop = parseInt(e.scrollTop);
    }
}
</script>

<style scoped>
.he-index-row {
    padding-left: 24px;

}
.he-index-row>.he-list-cell:last-child {
    border-bottom: none;
}
.he-list-cell {
    font-size: 28px;
    font-family: PingFang SC;
    font-weight: 500;
    color: #222222;
    height: 88px;
    line-height: 88px;
    background: #FFFFFF;
    border-bottom: 1px solid #E5E5E5;
}
</style>